package edu.neelsys.repository.usercourse;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.transaction.annotation.Transactional;

import com.mysema.query.hql.HQLQuery;
import com.mysema.query.hql.jpa.JPAQuery;

import edu.neelsys.objectmodel.dbentites.QUserCourse;
import edu.neelsys.objectmodel.dbentites.User;
import edu.neelsys.objectmodel.dbentites.UserCourse;

/**
 * Implementation of custom entity query methods. The example here uses QueryDsl
 * way so that queries are fully java complaint. However, there is a possiblity
 * to use named queries or simple string quereis as well here.
 * 
 * To Understand how to implement quereis do a search on QueryDsl examples in
 * google.
 * 
 * @author madhu
 * 
 */
public class UserCourseRepositoryImpl implements UserCourseRepoCustom {

	@PersistenceContext
	private EntityManager entityManager;

	@Override
	@Transactional
	public List<UserCourse> findCoursesByUser(User user) {
		QUserCourse userCourse = QUserCourse.userCourse;

		HQLQuery query = new JPAQuery(entityManager);

		List<UserCourse> userCourseList = query.from(userCourse)
				.where((userCourse.user.userName.eq(user.getUserName())))
				.list(userCourse);

		return userCourseList;
	}
}
